<?php

namespace Common\Controller;

use Think\Controller;

class BaseController extends Controller {

    protected $iUserId = 0; //用户ID
    /* protected $iPostId = 0; //所属岗位ID
    protected $iCompanyId = 0; //所属企业ID
    protected $iRoleId = 0; //角色ID(0-普通成员 1-老板 2-管理员) */

    public function _initialize() {
        $this->iUserId = session('iUserId');
    
    }

    /**
     * @param string $message 提示信息   
     * @param int $code 通用代码
     * @param array $data 自定义返回参数
     * @param string $jump_url 跳转URL
     * {@inheritDoc}
     * @see \Think\Controller::success()
     */
    public function success($message = '', $code = '', $data = array(), $jump_url = '') {
        $return = array('success' => true, 'message' => $message);
        $return['code'] = $code;
        $data['url'] = $jump_url;
        $return = $data ? array_merge($return, $data) : $return;
        $this->ajaxReturn($return);
    }

    /**
     * @param string $message 提示信息   
     * @param int $code 通用代码
     * @param array $data 自定义返回参数
     * @param string $jump_url 跳转URL
     * {@inheritDoc}
     * @see \Think\Controller::error()
     */
    public function error($message = '', $code = '', $data = array(), $jump_url = '') {
        $return = array('success' => false, 'message' => $message);
        $return['code'] = $code;
        $data['url'] = $jump_url;

        $return = $data ? array_merge($return, $data) : $return;
        $this->ajaxReturn($return);
    }

    /**
     * 返回列表数据
     * @param array $list 数据列表
     * @param int $page_no  当前页码
     * @param int $page_size    分页大小
     * @param int $count    总记录条数
     */
    public function returnList($list, $page_no = 0, $page_size = 0, $count = 0) {
        $page_total = ceil($count / $page_size);
        $return = array('success' => true, 'message' => '');
        $return['code'] = $list ? C('CODE_DATA_ISTRUE') : C('CODE_DATA_ISFALSE');
        if ($page_no && $page_size && $count) {
            $return['page'] = array(
                'page_current' => $page_no,
                'page_size' => $page_size,
                'page_total' => $page_total,
                'total' => $count
            );
        }
        $return['data'] = $list;
        $this->ajaxReturn($return);
    }
    /**
     * TODO 上传文件方法
     * @param $fileid form表单file的name值
     * @param $dir 上传到uploads目录的$dir文件夹里
     * @param int $maxsize 最大上传限制，默认1048576 byte = 1*1024*1024 byte = 1M
     * @param array $exts 允许上传文件类型 默认array('gif','jpg','jpeg','bmp','png')
     * @return array 返回array,失败status=0 成功status=1,filepath=newspost/2014-9-9/a.jpg
     */
    protected function uploadfile($fileid,$dir,$maxsize = 1048576,$exts = array('gif','jpg','jpeg','bmp','png','doc','docx')) {
        $upload           = new \Think\Upload();// 实例化上传类
        $upload->maxSize  = $maxsize;// 设置附件上传大小，单位字节(微信图片限制1M
        $upload->exts     = $exts;// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = $dir . '/'; // 设置附件上传（子）目录
        // 上传文件
    
        $info             = $upload->uploadOne($_FILES[$fileid]);
    
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }
        else {// 上传成功
            return '/Uploads/'.$info['savepath'] . $info['savename'];
        }
    }
}
